package com.itheima.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.mapper.UserMapper;
import com.itheima.utils.IdWorker;
import com.itheima.vo.req.LoginReqVo;
import com.itheima.vo.resp.LoginRespVo;
import com.itheima.vo.resp.R;
import com.itheima.vo.resp.ResponseCode;
import com.itheima.service.UserService;
import com.itheima.stock.pojo.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 用户登录
     * @param reqVo
     * @return
     */
    @Override
    public R<LoginRespVo> login(LoginReqVo reqVo) {
        String code = reqVo.getCode();
        String rkey = reqVo.getRkey();

        //验证码为空
        if (StringUtils.isEmpty(code)){
            //验证码不能为空
            return R.error(ResponseCode.CHECK_CODE_NOT_EMPTY);
        }
        Object o = redisTemplate.opsForValue().get("CK:" + rkey);
        //验证码过期
        if (o==null){
            //验证码过期
            return R.error(ResponseCode.CHECK_CODE_TIMEOUT);
        }
        //验证码有误
        if (!code.equals(o)){
            return R.error(ResponseCode.CHECK_CODE_ERROR);
        }

        //判断输入参数的合法性
        if (reqVo==null || StringUtils.isBlank(reqVo.getUsername()) || StringUtils.isBlank(reqVo.getPassword())) {
            return R.error(ResponseCode.USERNAME_OR_PASSWORD_ERROR);
        }
        //根据用户名查询
        SysUser sysUser = userMapper.getByUsername(reqVo.getUsername());
        //用户名为空||密码有误
        if (ObjectUtil.isEmpty(sysUser) || ! passwordEncoder.matches(reqVo.getPassword(), sysUser.getPassword())){
            return R.error(ResponseCode.USERNAME_OR_PASSWORD_ERROR);
        }
        //账号被锁定
        if (sysUser.getStatus()==0){
            return R.error(ResponseCode.ACCOUNT_LOCKED);
        }
        //复制属性
        LoginRespVo loginRespVo = new LoginRespVo();
        BeanUtils.copyProperties(sysUser,loginRespVo);
        return R.ok(loginRespVo);
    }

    /**
     * 生成验证码
     * @return
     */
    @Override
    public Map getCaptchaCode() {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 30);
        //生成验证码
        String code = lineCaptcha.getCode();
        //rkey:雪花算法  / UUID
        long rkey = new IdWorker().nextId();

        redisTemplate.opsForValue().set("CK:"+rkey,code,1, TimeUnit.MINUTES);
        final HashMap<Object, Object> codes = new HashMap<>();
        codes.put("code",code);
        //避免精度丢失
        codes.put("rkey",rkey+"");
        return codes;
    }
}











